home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1833 / 1833.xpi / chrome / yoono.jar / content / yoono / servicesweb.js < prev    next >
Text File  |  2009-12-16  |  12KB  |  300 lines

  1.  
  2.  
  3. function ynConnectException(msg){
  4.     this.message = msg;
  5.     this.toString = function (){return this.message};
  6. }
  7.  
  8. function ynResponseException(msg){
  9.     this.message = msg;
  10.     this.toString = function (){return this.message};
  11. }
  12.  
  13.  
  14. var ynWebServices = {
  15.     _serverUrl : '',
  16.     _sendRequest : function (script, libelle){
  17.  
  18.           // results is an array with request and xml result if successfull
  19.           var results = null;
  20.           YOONO_LOG.debug('Sending ' + libelle);
  21.           try{
  22.             results = YOONO_CMPT.sendRequest(this._serverUrl, 'POST', 'sync', script, null);     
  23.           } catch (e) {
  24.               throw new ynConnectException(e.toString());
  25.           }
  26.           if('' == results[1]) {
  27.               throw new ynResponseException(result[0].status);
  28.           }
  29.           return results[1];
  30.     },
  31.  
  32.     verifyLogin : function(login, passwd){
  33.         var script = <server-script version="1.0">
  34.                        <context>
  35.                          <user-id> {login + ':' + passwd} </user-id>
  36.                          <locale>{YOONO_UTILS.getLocale()}</locale>
  37.                          <version>{YOONO_UTILS.getExtensionVersion()}</version>
  38.                          <client>xpi</client>
  39.                        </context>
  40.                        <connect/>
  41.                      </server-script>;
  42.         var rep = this._sendRequest(script,"verifyLogin");
  43.  
  44.         // connection OK
  45.         if (rep['display-message'].@code == 'MSG_WELCOME') {
  46.             YNPREFBRANCH.setCharPref('userid', login + ':' + passwd);
  47.             YOONO_MEMO.fixLayout(window);
  48.             return true;
  49.         }else{
  50.             // login password invalid
  51.             return false;
  52.         }
  53.     },
  54.  
  55.     getNewUserId : function(){
  56.         var script = <server-script version="1.0">
  57.                       <connect />
  58.                       <context>
  59.                         <locale>{YOONO_UTILS.getLocale()}</locale>
  60.                         <version>{YOONO_UTILS.getExtensionVersion()}</version>
  61.                         <client>xpi</client>
  62.                       </context>
  63.                      </server-script>;
  64.         var rep = this._sendRequest(script,"getNewUserId");
  65.         if(rep['display-message'].@code == 'MSG_WELCOME_NEW_USER') {
  66.             var ac = YNPREFBRANCH.getIntPref('anonymous.creations');
  67.             YNPREFBRANCH.setIntPref('anonymous.creations', ac + 1);
  68.             YNPREFBRANCH.setIntPref('anonymous.creation.date', Math.round(new Date().getTime()/1000)) ;
  69.             return rep.context['user-id'].toString();
  70.         } else {
  71.             // login password invalid
  72.             return false;
  73.         }
  74.     },
  75.  
  76.     checkLoginDispo : function(pseudo){
  77.         var script= <server-script version="1.0"><get-user-data user-id={pseudo}/></server-script> ;
  78.         var rep = this._sendRequest(script,"checkLoginDispo");
  79.         if (rep['display-error'].@code == 'ERROR_INVALID_USER_ID')
  80.             return true;
  81.         else
  82.             return false;
  83.     },
  84.  
  85.     // @param nsIFile file
  86.     sendAvatar : function (userid, file) {    
  87.         // from http://xulfr.org/wiki/ApplisWeb/Request
  88.         var boundary="111222111"; 
  89.         const STRINGIS="@mozilla.org/io/string-input-stream;1";
  90.         const nsIStringInputStream=Components.interfaces.nsIStringInputStream;
  91.         
  92.         var fin=Components.classes["@mozilla.org/network/file-input-stream;1"]
  93.                 .createInstance(Components.interfaces.nsIFileInputStream);
  94.         var buf=Components.classes["@mozilla.org/network/buffered-input-stream;1"]
  95.                 .createInstance(Components.interfaces.nsIBufferedInputStream);
  96.  
  97.         fin.init(file,0x01,0444,0);
  98.         buf.init(fin,4096);
  99.         
  100.         var hsis=Components.classes[STRINGIS].createInstance(nsIStringInputStream);
  101.         var sheader=new String();
  102.         sheader+="\r\n";
  103.         sheader+="--"+boundary+"\r\nContent-disposition: form-data;name=\"user-id\"\r\n\r\n";
  104.         sheader+=userid;
  105.         sheader+="\r\n"+"--"+boundary+"\r\n";
  106.         sheader+="Content-disposition: form-data;name=\"image\";filename=\""+file.leafName+"\"\r\n";
  107.         sheader+="Content-Type: application/octet-stream\r\n";
  108.         sheader+="Content-Length: "+file.fileSize+"\r\n\r\n";
  109.         hsis.setData(sheader,sheader.length);
  110.         
  111.         var endsis=Components.classes[STRINGIS].createInstance(nsIStringInputStream);
  112.         var bs=new String("\r\n--"+boundary+"--\r\n");
  113.         endsis.setData(bs,bs.length);
  114.         
  115.         var mis=Components.classes["@mozilla.org/io/multiplex-input-stream;1"]
  116.                 .createInstance(Components.interfaces.nsIMultiplexInputStream);
  117.         mis.appendStream(hsis);
  118.         mis.appendStream(buf);
  119.         mis.appendStream(endsis);
  120.         YOONO_LOG.debug("\n==========================-->sendAvatar");
  121.         var xmlr = new XMLHttpRequest();
  122.         xmlr.open("POST",YNPREFBRANCH.getCharPref('serverurl') + "avatar",false);
  123.         xmlr.setRequestHeader("Content-Length",(mis.available()-2));
  124.         xmlr.setRequestHeader("Content-Type","multipart/form-data; boundary="+boundary);
  125.         xmlr.send(mis);
  126.         var imgValid;
  127.  
  128.         YOONO_LOG.debug("\n"+xmlr.responseText);
  129.         var xml = new XML(xmlr.responseText.replace(/<\?xml.*?\?>\n?/g, ""));
  130.  
  131.         if( xmlr.responseText.indexOf("INVALID_AVATAR") != -1 ) {
  132.             return false;
  133.         } else {
  134.            return xml['import-user-data']['user-data'].avatar.toString();
  135.         }
  136.     },
  137.  
  138.     // that's called only for anonymous to registered account transition.
  139.     changeLoginPassword: function (userid, login, password){
  140.         var script = <server-script version="1.0"/>;
  141.         script.appendChild( <context> <user-id> {userid} </user-id> </context>);;
  142.         script.appendChild( <change-login-password login={login} password={password}/>);
  143.  
  144.         var rep = this._sendRequest(script, "changeLoginPassword");
  145.         if (rep['display-message'].@code == 'MSG_LOGIN_PASSWORD_CHANGED') {
  146.           var uc = YNPREFBRANCH.getIntPref('user.creations');
  147.           YNPREFBRANCH.setIntPref('user.creations', uc + 1);
  148.           YNPREFBRANCH.setIntPref('user.creation.date', Math.round(new Date().getTime()/1000)) ;
  149.           return true;
  150.         } else {
  151.           return false;
  152.         }
  153.     },
  154.  
  155.     // Get user info, synchronously, for login/synch wizard
  156.     getUserData: function (userdata) {
  157.         var script = <server-script version="1.0"/> ;
  158.         script.appendChild( <context> <user-id>{userdata.userid}</user-id> </context>);
  159.         script.appendChild( <get-user-data/>);
  160.         var rep = this._sendRequest(script, "getUserData");
  161.         if (rep['display-error'].@code == 'ERROR_INVALID_USER_ID') {
  162.             return false;
  163.         }
  164.  
  165.         var data = rep['import-user-data']['user-data'];
  166.         userdata.pseudo = userdata.login;
  167.         userdata.name = data['full-name'].toString();
  168.         userdata.location = data['location'].toString();
  169.         userdata.born = data['born'].toString();
  170.         userdata.website = data['website'].toString();
  171.         // renseigner par defaut le champ avec l'URL de "Mon espace perso"
  172.         if (!userdata.website) {
  173.             userdata.website = 'http://www.yoono.com/favorites.jsp?user-id=' + userdata.login;
  174.         }
  175.         // Avatar in xml can be empty for old user who has an avatar !
  176.         userdata.avatar = data['avatar'].toString();
  177.  
  178.         userdata.description = data['message'].toString();
  179.         userdata.passions = data['interests'].toString();
  180.         userdata.email = data['email'].toString();
  181.         if (data['email'].@perm.toString().toLowerCase() == "owner") {
  182.             userdata.privateemail = true;
  183.         }else{
  184.             userdata.privateemail = false;
  185.         }
  186.         userdata.skype = data['skype-id'].toString();
  187.         userdata.msn = data['msn-id'].toString();
  188.         userdata.icq = data['icq-id'].toString();
  189.         userdata.aim = data['aim-id'].toString();
  190.  
  191.         return true;
  192.     },
  193.  
  194.     saveUserEmailAvatar : function(userData){
  195.         var script = <server-script version="1.0">
  196.             <context client-area="/"><user-id>{userData.userid}</user-id></context>
  197.             <save-user-data> <user-data>
  198.                     <email perm="OWNER">{userData.email}</email>
  199.                     <avatar>{userData.avatar}</avatar>
  200.             </user-data></save-user-data></server-script>;
  201.         var rep = this._sendRequest(script, "saveUserEmailAvatar");
  202.         if (rep['display-message'].@code == 'MSG_USER_DATA_SAVED') {
  203.             return true;
  204.         }else{
  205.             return false;
  206.         }
  207.     },
  208.  
  209.     saveUserData : function(infos){
  210.         var userdata = <user-data>
  211.             {(infos.name ? <full-name>{infos.name}</full-name> : '')}
  212.             {(infos.born ? <born>{infos.born}</born> : '')}
  213.             {(infos.location ? <location>{infos.location}</location> : '')}
  214.             {(infos.website ? <website>{infos.website}</website> : '')}
  215.             {(infos.description ? <message>{infos.description}</message> : '')}
  216.             {(infos.passions ? <interests>{infos.passions}</interests> : '')}
  217.             {(infos.email ? <email perm={infos.privateemail?"owner":""}>{infos.email}</email> : '')}
  218.             {(infos.skype ? <skype-id>{infos.skype}</skype-id> : '')}
  219.             {(infos.msn ? <msn-id>{infos.msn}</msn-id> : '')}
  220.             {(infos.icq ? <icq-id>{infos.icq}</icq-id> : '')}
  221.             {(infos.aim ? <aim-id>{infos.aim}</aim-id> : '')}
  222.             {(infos.avatar ? <avatar>{infos.avatar}</avatar> : '')}
  223.         </user-data>;
  224.  
  225.         var script = <server-script version="1.0">
  226.             <context client-area="/"><user-id>{infos.userid}</user-id></context>
  227.             <save-user-data>{userdata}</save-user-data></server-script>;
  228.         var rep = this._sendRequest(script, "saveUserData");
  229.         if (rep['display-message'].@code == 'MSG_USER_DATA_SAVED') {
  230.             return true;
  231.         }else{
  232.             return false;
  233.         }
  234.     },
  235.  
  236.     getSharedLink : function ( userid ) {
  237.         var script = <server-script version="1.0">
  238.             <context><user-id>{userid}</user-id></context>
  239.             <load-all-links published="true"/>
  240.         </server-script>;
  241.  
  242.         var rep = this._sendRequest(script, "getUserPublishedInfos");
  243.  
  244.         return rep['import-links'].elements('link');
  245.     },
  246.  
  247.     updateSharedLink : function (userid, newLinksToShare, linksToRemove){
  248.         var nbr = this.getPublishedCurrentMaxNumber(userid);
  249.         var script = <server-script version="1.0">
  250.             <context client-area="/"><user-id>{userid}</user-id>
  251.              </context></server-script>;
  252.  
  253.         for (var i in newLinksToShare) {
  254.             nbr++;
  255.             var item = newLinksToShare[i].node.getPath().toServer;
  256.             script.appendChild(<add-xlink ref={":" + nbr} path={"/$pub/" +nbr}/>)
  257.             var tmp = <update-link/>;
  258.             tmp.appendChild(<old-link path={item}/>);
  259.             tmp.appendChild(<new-link path={item} id={nbr}/>);
  260.             script.appendChild(tmp);
  261.         }
  262.         for (var i in linksToRemove) {
  263.             var item = linksToRemove[i];
  264.             script.appendChild(<remove-xlink ref={":" + item.publishedNbr} path={"/$pub/" +item.publishedNbr}/>)
  265.             var tmp = <update-link/>;
  266.             tmp.appendChild(<old-link path={item.node.getPath().toServer}/>);
  267.             tmp.appendChild(<new-link path={item.node.getPath().toServer} id={item.publishedNbr}/>);
  268.             script.appendChild(tmp);
  269.         }
  270.  
  271.         var rep = this._sendRequest(script, "updateSharedLink");
  272.         return true;
  273.     },
  274.  
  275.  
  276.     getPublishedCurrentMaxNumber : function(userid) {
  277.  
  278.         var script = <server-script version="1.0">
  279.                 <context> <user-id> {userid} </user-id> </context>
  280.                 <load-all-xlinks/>
  281.                 </server-script>;
  282.         var rep = this._sendRequest(script, "getPublishedCurrentMaxNumber");
  283.  
  284.         var links = rep['import-links'].elements('link');
  285.  
  286.         var nbr = 0;
  287.         for (var i = links.length(); i-- > 0;) {
  288.             if (links[i].@path.toString().beginsWith('/$net'))
  289.                 continue;
  290.             var ref = links[i].@ref.toString();
  291.             var id = parseInt(ref.slice(ref.indexOf(':') + 1));
  292.             if (id > nbr) nbr = id;
  293.         }
  294.        return nbr;
  295.     }
  296.  
  297. }
  298.  
  299. ynWebServices._serverUrl = YNPREFBRANCH.getCharPref('serverurl')+ 'linkserver';
  300.